package chen.web.forum;

import java.util.Date;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import chen.web.DataSourceProvider;
import chen.web.MyContextProvider;
import chen.web.event.EventListener;
import chen.web.forum.event.BasePostEvent;
import chen.web.forum.event.PageViewEvent;
import chen.web.forum.event.PostCheckedEvent;
import chen.web.forum.event.PostDeletedEvent;
import chen.web.forum.event.PostPublishedEvent;
import chen.web.forum.event.PostRestoredEvent;
import chen.web.forum.event.PostType;
import chen.web.util.DateInt;

public class ForumAccountListeners {

	public static class PostPublishedListener implements EventListener<PostPublishedEvent>{

		@Override
		public void listen(PostPublishedEvent event) {
			// TODO Auto-generated method stub
			switch(event.getPostType()){
			case Subject : {
				Subject subject = (Subject)event.getEntity();
				if(subject.isNeedChecking()) return; break;
			}
			case Reply :{
				Reply reply = (Reply)event.getEntity();
				if(reply.isNeedChecking()) return; break;
			}
			}
			
			ForumAccount author = CategoryDynamicInfoListeners.getAuthor(event);
			Date lastPostDate = CategoryDynamicInfoListeners.getPostDate(event);
			author.setLastPostDate(lastPostDate);
			changePostCount(event, true);
			persist(author);
		}
		
	}
	
	public static class PostDeletedListener implements EventListener<PostDeletedEvent>{

		@Override
		public void listen(PostDeletedEvent event) {
			// TODO Auto-generated method stub
			boolean fakeDeleted = false;
			switch(event.getPostType()){
			case Subject :{
				fakeDeleted = ((Subject)event.getEntity()).isFakeDelete();
				break;
			}
			case Reply :{
				fakeDeleted = ((Reply)event.getEntity()).isFakeDelete();
				break;
			}
			}
			//已经假删除过了，此次真删除不操作
			if(fakeDeleted && !event.isFake()){
				return;
			}
			changePostCount(event, false);
			persist(CategoryDynamicInfoListeners.getAuthor(event));
		}
		
	}
	
	public static class PostCheckedListener implements EventListener<PostCheckedEvent>{

		@Override
		public void listen(PostCheckedEvent event) {
			// TODO Auto-generated method stub
			changePostCount(event, true);
			persist(CategoryDynamicInfoListeners.getAuthor(event));
		}
		
	}
	
	private static void changePostCount(BasePostEvent event, boolean increase){
		ForumAccount author = CategoryDynamicInfoListeners.getAuthor(event);
		if(event.getPostType() == PostType.Subject)
			author.changeSubjectCount(increase);
		else
			author.changeReplyCount(increase);
		persist(author);
	}
	 
	public static class PostRestoredListener implements EventListener<PostRestoredEvent>{

		@Override
		public void listen(PostRestoredEvent event) {
			// TODO Auto-generated method stub
			changePostCount(event, true);
			persist(CategoryDynamicInfoListeners.getAuthor(event));
		}
		
	}
	
	public static class PageViewListener implements EventListener<PageViewEvent>{

		private ForumAccountLoader loader;
		public PageViewListener(ForumAccountLoader loader){
			this.loader = loader;
		}
		
		@Override
		public void listen(PageViewEvent event) {
			// TODO Auto-generated method stub
			ForumAccount acc = loader.loadForumAccount(MyContextProvider.getMyContext().getAccount());
			acc.increasePageView();
			persist(acc);
		}
		
	}
	
	private static final String SQL_1 = "update web_account_forum set af_replycount =?, af_subjectCount=?, af_digestCount=?, af_pageView=?, af_lastPostDate = ? where acc_id = ?";
	private static SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(DataSourceProvider.getDataSource());

	private static void persist(ForumAccount account){
		simpleJdbcTemplate.update(SQL_1, account.getReplyCount(), account.getSubjectCount(), 
				account.getDigestCount(), account.getPageView(), DateInt.transform(account.getLastPostDate()), account.getId());
	}
}
